<?php

/**
 * AdminIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class AdminIdentity extends WebUserIdentity
{
	protected $isAdmin = true;
	
	public function authenticate()
	{
		$record = Admin::model()->findByAttributes(array('username' => $this->username));
        if ($record === null) {
            $this->errorCode = self::ERROR_USERNAME_INVALID;
        } else if ($record->password !== md5($this->password)) {
            $this->errorCode = self::ERROR_PASSWORD_INVALID;
        } else {
            $this->setState('role', 'backend');
            $this->errorCode = self::ERROR_NONE;
			$this->id = $record->id;
			$this->userModel = $record;
			$this->userName = $record->username;
			
			$record->last_login = date('Y-m-d H:i:s');
			$record->last_login_ip = Yii::app()->request->getUserHostAddress();
			// Update last IP and time
			$record->save();
        }
        return!$this->errorCode;
	}
}